package com.esDao.impl;

import com.entity.User;
import com.esDao.EmpDao;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Repository
public class EmpDaoImpl implements EmpDao {
    @Autowired
    private TransportClient client;
    @Override
    public List<User> searchHighLighter(String content) {
        SearchResponse searchResponse = client.prepareSearch("ems").setTypes("emp")
                .setQuery(QueryBuilders.multiMatchQuery(content, "name", "content"))
                .highlighter(new HighlightBuilder().field("content").field("name").preTags("<span style='color:red'>").postTags("</span>")).get();
        SearchHits hits = searchResponse.getHits();
        List<User> users = new ArrayList<>();
        for(SearchHit hit:hits){
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            User user = new User();
            String idStr = hit.getId();
            user.setId(Integer.parseInt(idStr));
            user.setName((String)sourceAsMap.get("name"));
            user.setAge((Integer) sourceAsMap.get("age"));
            String birStr = (String)sourceAsMap.get("bir");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = new Date();
            try {
                date = sdf.parse(birStr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            user.setBir(date);
            user.setContent((String)sourceAsMap.get("content"));
            user.setAddress((String)sourceAsMap.get("address"));
            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
            HighlightField content2 = highlightFields.get("content");
            if(content2 != null){
                Text text = content2.fragments()[0];
                user.setContent(text.toString());
            }
            HighlightField content3 = highlightFields.get("name");
            if(content3 != null){
                Text text = content3.fragments()[0];
                user.setName(text.toString());
            }
            users.add(user);
        }
        return users;
    }

    @Override
    public void insertUser(User user) {
        Map map = new HashMap();
        map.put("name",user.getName());
        map.put("age",user.getAge());
        Date bir = user.getBir();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = sdf.format(bir);
        map.put("bir",dateStr);
        map.put("content",user.getContent());
        map.put("address",user.getAddress());
        client.prepareIndex("ems", "emp", user.getId()+"").setSource(map).get();
    }

    @Override
    public void deleteUserById(Integer id) {
        client.prepareDelete("ems", "emp", id+"").get();
    }

    @Override
    public void updateUser(User user) {
        Map map = new HashMap();
        map.put("name",user.getName());
        map.put("age",user.getAge());
        Date bir = user.getBir();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = sdf.format(bir);
        map.put("bir",dateStr);
        map.put("content",user.getContent());
        map.put("address",user.getAddress());
        client.prepareUpdate("ems", "emp", user.getId()+"").setDoc(map).get();
    }
}
